from django import template
from django.utils.html import format_html
register = template.Library()

@register.simple_tag
def cheng(var1,var2):
    res = float(var1) * float(var2)

    return '%.2f'%res


# 自定义分页优化标签
@register.simple_tag
 
#views视图处理数据,将数据通过{ 加载 }方式给列表的html,在列表里调用过滤器即函数并传递参数count和request对象中的p
def PageShow(count,request):
    p = int(request.GET.get('p',1))
    begin = p-4
    end = p+5
    if p > count-5:
        begin = count-9
        end = count
    if p < 5:
        begin = 1
        end = 10
    if count < 10:
        begin = 1
        end = count
    #获取当前请求的url
    #print(request)
    url = request.path
    args = ''
    for k,v in request.GET.items():
        if k != 'p':
            args += '&'+k+'='+v
    s = ''
    #首页
    s += '<li><a href="{url}?p={v}{args}">首页</a></li>'.format(v=1,url=url,args=args)
    #判断上一页
    if p ==1:
        s += '<li ><a href ="{url}?p={v}{args}"> 上一页 </a></li>'.format(v = 1,url =url,args =args)   
    else:
        s += '<li ><a href ="{url}?p={v}{args}"> 上一页 </a></li>'.format(v = p-1,url =url,args =args)   
    #循环页码数   
    for x in range(begin,end+1):
        if x == p:
            s += '<li class="am-active"><a href ="{url}?p={v}{args}">{v}</a></li>'.format(v = x,url =url,args =args)   
        else:
            s += '<li ><a href ="{url}?p={v}{args}">{v}</a></li>'.format(v = x,url =url,args =args)   
     
    # 判断下一页
    if p == count:
        s += '<li ><a href ="{url}?p={v}{args}"> 下一页 </a></li>'.format(v = count,url =url,args =args)   
    else:
        s += '<li ><a href ="{url}?p={v}{args}"> 下一页 </a></li>'.format(v = p+1,url =url,args =args)

    # 尾页
    s += '<li ><a href ="{url}?p={v}{args}">尾页</a></li>'.format(v = count,url =url,args =args)           
    # 解析HTML,防止出现的是html源码
    return format_html(s)


 
#views视图处理数据,将数据通过{ 加载 }方式给列表的html,在列表里调用过滤器即函数并传递参数count和request对象中的p
